package com.lupus.util;

import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.util.Random;

public class ValidateUtil {
	private int height = 20;
	private int width = 60;
	private int coderCount = 4;
	private int lineCount = 15;
	private char[] coderArray = {'A','B','C','D','E','F','G','H','I','J',
	'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
	
	public Map<String,Object> generateValidateImage(){
		int x = width/(coderCount);
		int y = height-4; 
		int fontHeight = height-2;
		Map<String,Object> map = new HashMap<String,Object>();
		BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 
		Graphics2D g = buffImg.createGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0,0,width,height);
		Font font = new Font("Fixedsys",Font.PLAIN,fontHeight);
		g.setFont(font);
		g.setColor(Color.BLACK);
		g.drawRect(0,0,width-1,height-1);
		g.setColor(Color.BLACK);
		Random random = new Random();
		for(int i=0;i<lineCount;i++){
			int x1 = random.nextInt(width); 
			int y1 = random.nextInt(height); 
			int x2 = random.nextInt(12); 
			int y2 = random.nextInt(12); 
			g.drawLine(x1,y1,x1+x2,y1+y2);
		}
		StringBuffer randomCode = new StringBuffer();
		int length = coderArray.length;
		for (int i=0;i<coderCount;i++) {
			String str = String.valueOf(coderArray[random.nextInt(length)]);
			g.setColor(Color.red);
			g.drawString(str,i*x+2,y);
			randomCode.append(str);
		}
		map.put("validateImage", buffImg);
		map.put("validateCoder", randomCode.toString());
		return map;
	}
}
